/** 
*
* @author  jiwf 
* @date 创建时间：2016年10月21日 下午3:12:14 
* @version 1.0 
* @since  
*  
*/
package com.xbstar.web;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.xbstar.bean.TripRouteInfo;
import com.xbstar.dao.AffixInfoRepository;
import com.xbstar.dao.TripRouteInfoRepository;
import com.xbstar.tools.BeanUtils;
import com.xbstar.tools.Utils;

@RestController
@RequestMapping("/trip")
@CrossOrigin
public class TripInfoController {
	@Autowired
	TripRouteInfoRepository tripRe;
	@Autowired
	AffixInfoRepository affixRe;
	@RequestMapping("/add")
	public String addTripInFo(@RequestParam("affixBelongTo")String affixBelongTo,HttpServletRequest req){
		JSONObject json=new JSONObject();
		TripRouteInfo trip=new TripRouteInfo();
		try {
			trip=BeanUtils.copyRequestToObj(trip, req);
			if(trip.getTripBelongTo()==null||trip.getTripBelongTo().length()<1){
				trip.setTripBelongTo(Utils.getUUID());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		tripRe.save(trip);
		JSONObject datajson=BeanUtils.getAJsonFromObj(trip, new String[]{"id","tripBelongTo"});
		if(trip==null||trip.getId()<1||datajson==null){
			json.put(UserPackage.STATUS, UserPackage.SERVER_ERROR);
		}else{
			json.put(UserPackage.STATUS, UserPackage.SUCCESS);
			json.put(UserPackage.DATA, datajson);
		}
		return json.toString();
	}
	@RequestMapping("/edit")
	public String editTripInFo(@RequestParam("id")Long id,HttpServletRequest req){
		JSONObject json=new JSONObject();
		TripRouteInfo trip=new TripRouteInfo();
		try {
			TripRouteInfo older=tripRe.findOne(id);
			trip=BeanUtils.copyRequestToObj(trip, req);
			trip=BeanUtils.merge(older, older);
			tripRe.save(trip);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		JSONObject datajson=BeanUtils.getAJsonFromObj(trip, new String[]{"id","tripBelongTo"});
		if(trip==null||trip.getId()<1||datajson==null){
			json.put(UserPackage.STATUS, UserPackage.SERVER_ERROR);
		}else{
			json.put(UserPackage.STATUS, UserPackage.SUCCESS);
			json.put(UserPackage.DATA, datajson);
		}
		return json.toString();
	}
	@RequestMapping("/del")
	public String delTripInFo(@RequestParam("id")Long id,HttpServletRequest req){
		JSONObject json=new JSONObject();
		try {
			tripRe.delete(id);
			json.put(UserPackage.STATUS, UserPackage.SUCCESS);
			String affixId=req.getParameter("affixId");
			if(affixId!=null){
				try {
					affixRe.delete(Long.parseLong(affixId));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put(UserPackage.STATUS, UserPackage.REDO);
		}
		
		return json.toString();
	}
}
